<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    label-width="100px"
    v-loading="formLoading"
  >
    <el-divider content-position="left">推荐</el-divider>
    <el-form-item label="会员" prop="matchUser">
      <el-col :span="3" class="mr-2">
        <el-input v-model="formData.matchUser" v-onlyNumber placeholder="请输入会员匹配度" />
      </el-col>
      %
    </el-form-item>
    <el-form-item label="非会员" prop="matchNonUser">
      <el-col :span="3" class="mr-2">
        <el-input v-model="formData.matchNonUser" v-onlyNumber placeholder="请输入非会员匹配度" />
      </el-col>
      %
    </el-form-item>
    <el-divider content-position="left">非会员喜欢次数设置</el-divider>
    <el-form-item label="喜欢次数" prop="likeNum">
      免费
      <el-col :span="3" class="ml-2 mr-2">
        <el-input v-model="formData.likeNum" v-onlyNumber placeholder="请输入喜欢次数" />
      </el-col>
      次/天
    </el-form-item>
    <el-divider content-position="left">人工服务时间</el-divider>
    <el-form-item label="上午开始" prop="morningBegin">
      <el-time-picker
        v-model="formData.morningBegin"
        type="date"
        value-format="x"
        placeholder="请选择上午开始"
      />
    </el-form-item>
    <el-form-item label="上午结束" prop="morningEnd">
      <el-time-picker
        v-model="formData.morningEnd"
        type="date"
        value-format="x"
        placeholder="请选择上午结束"
      />
    </el-form-item>
    <el-form-item label="下午开始" prop="afternoonBegin">
      <el-time-picker
        v-model="formData.afternoonBegin"
        type="date"
        value-format="x"
        placeholder="请选择下午开始"
      />
    </el-form-item>
    <el-form-item label="下午结束" prop="afternoonEnd">
      <el-time-picker
        v-model="formData.afternoonEnd"
        type="date"
        value-format="x"
        placeholder="请选择下午结束"
      />
    </el-form-item>
    <el-form-item label="评论权限" prop="commentAuth">
      <el-radio-group v-model="formData.commentAuth">
        <el-radio :label="0" size="large">仅好友</el-radio>
        <el-radio :label="1" size="large">全部人</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="审核时限" prop="processTime">
      评论等待人工审核时限为
      <el-col :span="3" class="ml-2 mr-2">
        <el-input v-model="formData.processTime" v-onlyNumber placeholder="请输审核时限" />
      </el-col>
      分钟
    </el-form-item>
    <el-form-item label="报名人数" prop="enrollNumber">
      <el-col :span="3" class="ml-2 mr-2">
        <el-input-number v-model="formData.enrollNumber" :step="2" :min="6" step-strictly />
      </el-col>
      报名人数≥6且为偶数
    </el-form-item>
  </el-form>
  <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
</template>

<script setup lang="ts">
import * as BasicSettingApi from '@/api/blind/basicSetting'

defineOptions({ name: 'BasicSetting' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const formRef = ref() // 表单 Ref
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('create') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  matchUser: undefined,
  matchNonUser: undefined,
  likeNum: undefined,
  morningBegin: undefined,
  morningEnd: undefined,
  afternoonBegin: undefined,
  afternoonEnd: undefined,
  commentAuth: undefined,
  processTime: undefined,
  commissionRatio: undefined,
  effectiveTime: undefined,
  enrollNumber: undefined,
  status: undefined
})

const formRules = reactive({
  matchUser: [{ required: true, message: '会员推荐不能为空', trigger: 'blur' }],
  matchNonUser: [{ required: true, message: '非会员推荐不能为空', trigger: 'blur' }],
  likeNum: [{ required: true, message: '喜欢次数不能为空', trigger: 'blur' }],
  morningBegin: [{ required: true, message: '人工服务上午开始时间不能为空', trigger: 'blur' }],
  morningEnd: [{ required: true, message: '人工服务上午结束时间不能为空', trigger: 'blur' }],
  afternoonBegin: [{ required: true, message: '人工服务下午开始时间不能为空', trigger: 'blur' }],
  afternoonEnd: [{ required: true, message: '人工服务下午结束时间不能为空', trigger: 'blur' }],
  commentAuth: [{ required: true, message: '评论权限 0仅好友 1全部人不能为空', trigger: 'blur' }],
  processTime: [{ required: true, message: '审核时限不能为空', trigger: 'blur' }],
  enrollNumber: [{ required: true, message: '报名人数不能为空', trigger: 'blur' }]
})

/** 查询列表 */
const getData = async () => {
  const data = await BasicSettingApi.getBasicSetting(1)

  formData.value = data ? data : formData.value
  formType.value = data ? 'update' : 'create'
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true

  try {
    const data = formData.value as unknown as BasicSettingApi.BasicSettingVO

    if (formType.value === 'create') {
      await BasicSettingApi.createBasicSetting(data)
      message.success(t('common.createSuccess'))
    } else {
      await BasicSettingApi.updateBasicSetting(data)
      message.success(t('common.updateSuccess'))
    }
    // 发送操作成功的事件
    emit('success')
    getData()
  } finally {
    formLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getData()
})
</script>
